<?php
/*
 * Last Edited: 27-7-2013
 */

class AuthModel{
	function setIdentity($identity, $email, $first_name, $last_name){
		Zend_Loader::loadClass("AdminModel");
		$adminModel = new AdminModel();
		$admin_info = $adminModel->fetchRow(array("gome_email =?"=>trim($email)));	
		
		$session = new Zend_Session_Namespace();
		$identity_arr = array(
			'identity' => $identity,
			'id' => $admin_info['id'],
			'gome_email' => $email,
			'first_name' => $first_name,
			'last_name' =>$last_name
		);
		
		$session->identity = $identity_arr;
	}
	
	function hasIdentity(){
		$session = new Zend_Session_Namespace();
		return $session->identity ? true : false;
	}
	
	function getIdentity(){
		$session = new Zend_Session_Namespace();
		return $session->identity;
	}
	
	function clearIdentity(){
		$session = new Zend_Session_Namespace();
		unset($session->identity);
	}
	
	function getIdentityField($key){
		if($this->hasIdentity()){
			$identity = $this->getIdentity();
			return $identity[$key];
		}else 
			return NULL;
	}
	
	function validateUser($email){
		Zend_Loader::loadClass('AdminModel');
		
		$result = preg_match("/(.)*@gome.vn/",trim($email));
		
		if($result){
			//check user is in user list or not
			$model = new AdminModel();
			
			$validator = new Zend_Validate_Db_RecordExists(array(
																	'table'=>'admin',
																	'field'=>'gome_email',
																	'exclude' => 'status=1'
																	));
			if ($validator->isValid(trim($email))) {
				return true;
			}else 
				return false;
		}
		else 
			return false;
	}
}